<?php
	
/* 
	
	dkpm - a DKP management system
	
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/

	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Copyright (C) 2007 Martin Wedvich. All Rights Reserved.
	
	-
	
	framework.php
	This is the skeleton of the site, where all the repetitive code that is
	included on every page is applied.

*/

	if(!defined('dkpm')) {
		die();
	}
	
	require 'cfg.php';
	
	// this is a little flag that's enabled on the error page to avoid a redundant loop
	if(!$framework_nodb) {
		if(!mysql_connect($cfg['mysql']['host'], $cfg['mysql']['username'], $cfg['mysql']['password']) ||
		!mysql_select_db($cfg['mysql']['database'])) {		
		 	header('Location: error.php?error=2');
		 	exit;
		}
	}
	
	// "Remember me" functionality	
	if(!isset($_SESSION['user']['id'])) {
		if(isset($_COOKIE['dkpm'])) {
			$sql = mysql_query('SELECT user_id, username, user_email FROM dkpm_users WHERE user_session="' . $_COOKIE['dkpm'] . '"');
			if($row = mysql_fetch_row($sql)) {
			 	$_SESSION['user']['id'] = $row[0];
			 	$_SESSION['user']['name'] = $row[1];
			 	$_SESSION['user']['email'] = $row[2];
				setcookie('dkpm', session_id(), time() + 31104000);	
			 	mysql_query('UPDATE dkpm_users SET user_last_login=NOW() WHERE user_id=' . $row[0]);
			 	mysql_query('UPDATE dkpm_users SET user_last_ip="' . get_client_ip() . '" WHERE user_id=' . $row[0]);
			 	mysql_query('UPDATE dkpm_users SET user_session="' . session_id() . '" WHERE user_id=' . $row[0]);			 
			} else {
				setcookie('dkpm', '', time() - 3600);
			}
		}
	}

	
	function framework_exec($title, $content) {	
		if(isset($_SESSION['user']['id'])) {
			$usermenu = '
						<li class="spacer">|</li>
						<li><a class="tabbed" href="cp.php" title="View your control panel">User CP</a></li>
						<li class="spacer">|</li>
						<li><a class="tabbed" href="logout.php" title="Logout">Logout</a></li>';
		} else {
			$usermenu = '
						<li class="spacer">|</li>
						<li><a class="tabbed" href="login.php" title="Login">Login</a></li>';
		}	
		xhtml_if_available();
		echo '<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
	<head>
		<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
		<link rel="stylesheet" type="text/css" href="styles/lime/lime.css" title="dkpm Lime" media="screen" />	
		<title>' . $title . '</title>
		<!--[if IE]>
			<link rel="stylesheet" href="iefix.css" type="text/css" media="screen">
			<script type="text/javascript">onload = function() { top.focus(); }</script>
		<![endif]-->
	</head>
	<body>
		<div id="top">
			<div id="header">
				<div id="headertext">User <span class="medium">dkpm</span></div>
				<div id="tabs">
					<ul>
						<li class="tabbed"><a class="tabbed" href="#" title="User">User</a></li>
						<li class="tab"><a class="tab" href="#" title="Admin">Admin</a></li>
					</ul>
				</div>
				<div id="menu">
					<ul>
						<li><a class="tabbed" href="index.php" title="Index">Index</a></li>
						<li class="spacer">|</li>
						<li><a class="tabbed" href="standings.php" title="View DKP standings">Standings</a></li>
						<li class="spacer">|</li>
						<li><a class="tabbed" href="raids.php" title="View raids">Raids</a></li>
						<li class="spacer">|</li>
						<li><a class="tabbed" href="logs.php" title="View combat logs">Combat logs</a></li>' . $usermenu . '
					</ul>
				</div>
			</div>
			<div id="content">
				' . $content . '
			</div>
		</div>
		<div id="footer">
			<ul>
				<li class="left"><a class="link2" href="javascript:scroll(0, 0);">Top</a></li>
				<li><span class="dark">Generated in ' . round(microtime(), 3) . ' seconds</span></li>
			</ul>
		</div>
	</body>
</html>';	
		mysql_close();
	}

?>